//
//  PopularHistoryViewController.m
//  有材网
//
//  Created by qijia on 16/1/21.
//  Copyright © 2016年 肖扬. All rights reserved.
//

#import "PopularHistoryView.h"
#import "QJCellConfig.h"
#import "supplyViewButton.h"
@interface PopularHistoryView ()
{
    UIView *hotView;
    UIView *historyView;
}
@end

@implementation PopularHistoryView
@synthesize delegate;
-(void)drawRect:(CGRect)rect{
    [self makeHotUI];
    [self makeHistoryUI];
}

#pragma mark - 热门搜索

-(void)makeHotUI{
    hotView = [[UIView alloc]init];
    hotView.userInteractionEnabled = YES;
    hotView.backgroundColor = [UIColor clearColor];
    hotView.frame = CGRectMake(0, 0, kWIDTH, 110);
    [self addSubview:hotView];
    
    UILabel *hotLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 30)];
    hotLbl.text = @" 热门搜索";
    hotLbl.backgroundColor = UIColorFromRGB(0xf7f7f7);
    hotLbl.textColor = [UIColor blackColor];
    hotLbl.font = [UIFont systemFontOfSize:16];
    hotLbl.textAlignment = NSTextAlignmentLeft;
    [hotView addSubview:hotLbl];
    double h = 0;
    supplyViewButton *btnTemp = nil;
    for(int i = 0; i <[[appStatus shareAppStatus].hot_keywords count] ; i++){
        h = 50;
        supplyViewButton *btn = nil;
        int BtnW = (SWIDTH/4);
        int BtnH = 34;
        btn = [self returnButtonWithFrame:CGRectMake( BtnW * (i%4) ,40+ BtnH *(i/4), BtnW, BtnH ) withTag:i withTitle:[[appStatus shareAppStatus].hot_keywords objectAtIndex:i]];
        [hotView addSubview:btn];
        btnTemp = btn;
    }
    
}
-(supplyViewButton *)returnButtonWithFrame:(CGRect)frame  withTag:(int)Tag withTitle:(NSString *)title{
    supplyViewButton *vButton = [[supplyViewButton alloc]initWithFrame:frame];
    [vButton.button setTitle:title forState:0];
    [vButton.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [vButton.button setFrame:CGRectMake((frame.size.width - 70)/2, 0, 70, 24)];
    vButton.button.titleLabel.font = [UIFont systemFontOfSize:12];
    [vButton.button setTitleColor:UIColorFromRGB(0x2578c3) forState:UIControlStateSelected];
    [vButton.button setBackgroundColor:[UIColor clearColor]];
    Radio(vButton.button, grayColor);
    [vButton.button setSelected:NO];
    vButton.button.tag = Tag;
    vButton.tag = Tag;
    [vButton.button addTarget:self action:@selector(hotBtn:) forControlEvents:UIControlEventTouchUpInside];
    return vButton;
}
-(void)hotBtn:(supplyViewButton *)sender{
    UIButton *button = (UIButton *)[sender viewWithTag:sender.tag];
    if(self.h_block){
        self.h_block([button currentTitle],sender.tag);
    }
}

#pragma mark - 历史搜索
-(void)makeHistoryUI{
    //测试一下 先关闭
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"searchList"] isKindOfClass:[NSString class]]) {
        return;
    }
    NSMutableArray *hisArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"searchList"];
    if(hisArray.count != 0){
        historyView = [[UIView alloc]init];
        historyView.backgroundColor = [UIColor clearColor];
        historyView.frame = CGRectMake(0, hotView.bottom, kWIDTH, VHEIGHT-hotView.bottom-49);
        [self addSubview:historyView];
    
        UILabel *hotLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 30)];
        hotLbl.text = @" 历史记录";
        hotLbl.backgroundColor = UIColorFromRGB(0xf7f7f7);
        hotLbl.textColor = [UIColor blackColor];
        [hotLbl setTag:101];
        hotLbl.font = [UIFont systemFontOfSize:16];
        hotLbl.textAlignment = NSTextAlignmentLeft;
        [historyView addSubview:hotLbl];
    
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.frame = CGRectMake(0, 30, kWIDTH, VHEIGHT-hotView.bottom-37);
            m_tableView = [[QJUIMapping mapping] tableControlWithTableView:self.tableView withDelegate:self withContainer:self];
        [historyView addSubview:self.tableView];
    
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWIDTH, 50)];
        view.backgroundColor = [UIColor clearColor];
        self.tableView.tableFooterView = view;
        
        UIButton *clearButton = [UIButton buttonWithType:0];
        clearButton.frame = CGRectMake(0, 0, SWIDTH, 43);
        clearButton.backgroundColor = [UIColor clearColor];
        [clearButton setTitle:@"清空搜索记录" forState:0];
        [clearButton setTitleColor:BLUEColor forState:0];
        clearButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [clearButton addTarget:self action:@selector(clearHistoryView) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:clearButton];
        [self initDataWithControl:m_tableView];
    }

}
-(void)clearHistory{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"searchList"];
//    []
}
-(void)clearHistoryView{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"searchList"];
    if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(clearHisView)]) {
        [self.delegate clearHisView];
    }
}

-(void)initDataWithControl:(QJTableView *)control{
//正式数据
    NSMutableArray *hisArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"searchList"];
    [self.cellArray addObjectsFromArray:[QJCellConfig arrayFromDataArray:(NSMutableArray *)hisArray className:@"HistoryViewCell"]];
    [self reloadDatas];
}

-(void)onGoCellSelect:(NSIndexPath *)indexPath withObject:(id)object withControl:(id)control{
    NSLog(@"index___%d__object__%@___control___%@",indexPath.row,object,control);
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(hot_keyword:)]) {
        [self.delegate hot_keyword:object];
    }
}
//-(CGFloat)onTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 40;
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
